package org.medicine.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.log4j.Logger;
import org.medicine.model.Medicine;
import org.medicine.service.MedicineService;
import org.medicine.vo.MedicineVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@Component("medicine")
@Scope("prototype")
public class MedicineAction extends MyActionSupport implements ModelDriven<MedicineVO> {

    private final static Logger logger = Logger.getLogger(LoginAction.class);
    private MedicineService medicineService;
    private MedicineVO medicineVO = new MedicineVO();
    private Medicine medicine = new Medicine();

    //增加药品
    @Override
    public String execute() throws Exception {
        if ("".equals(medicineVO.getMedicineName())) {
            medicineVO.setResultMessage("请输入对应的药品名称！");
            return ERROR;
        }
        medicine = medicineService.findMedicineByName(medicineVO.getMedicineName());
        if (medicine != null) {
            medicineVO.setResultMessage("该药品信息已存在！");
            return ERROR;
        }
        medicine = copyDefaultMedicineInfo(new Medicine(), medicineVO);

        try {
            medicineService.save(medicine);
        } catch (Exception e) {
            medicineVO.setResultMessage("药品信息填写有误！");
            logger.error(e);
            return ERROR;
        }
        medicineVO.setResultMessage("添加药品信息成功！");
        logger.trace("Add Medicine:" + medicine.getMedicineName());
        return SUCCESS;
    }

    //查找药品
    public String loadMedicine() {
        try {
            medicine = medicineService.find(Medicine.class,medicineVO.getId());
        } catch (Exception e) {
            logger.error(e);
            medicineVO.setResultMessage("药品信息读取失败!");
            return ERROR;
        }
        medicineVO.setMedicine(medicine);
        view = "/page/Medicine/UpdateMedicine.jsp";
        return VIEW;
    }

    //修改药品信息
    public String updateMedicine() {
        medicine = medicineService.find(Medicine.class, medicineVO.getId());
        if (medicine == null) {
            medicineVO.setResultMessage("找不到改药品！");
            return ERROR;
        }

        try {
            medicineService.update(copyDefaultMedicineInfo(medicine, medicineVO));
        } catch (Exception e) {
            logger.error(e);
            medicineVO.setResultMessage("药品信息修改失败!");
            return ERROR;
        }
        medicineVO.setResultMessage("药品信息修改成功!");
        return SUCCESS;
    }

    //药品模糊查找
    public String fuzzySearch() {
        if ("".equals(medicineVO.getValue())) {
            medicineVO.setResultMessage("请输入查询的药品相关属性！");
            return ERROR;
        }
        List<Medicine> medicines = medicineService.findMedicines(medicineVO.getValue());
        medicineVO.setMedicines(medicines);
        medicineVO.setSearch(medicineVO.getValue());
        //判断是否为管理员
        if(medicineVO.getFlag().equals("admin")){
            view = "/page/Medicine/Medicinelist.jsp";
            return VIEW;
        }
        view = "/page/Medicine/FuzzySearchMedicine.jsp";
        return VIEW;
    }

    //查看所有药物
    public String loadAllMedicines() {
        List<Medicine> medicines = medicineService.getScrollData(Medicine.class).getResultList();
        medicineVO.setMedicines(medicines);
        view = "/page/Medicine/Medicinelist.jsp";
        return VIEW;
    }

    //删除药品信息
    public String deleteMedicine() {
        int ids[];
        try {
            ids = medicineVO.getMedicinesIds();
            for(int id : ids){
                medicineService.delete(Medicine.class,id);
            }
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            medicineVO.setResultMessage("药品信息删除失败!");
            return ERROR;
        }
        medicineVO.setResultMessage("药品信息删除成功!");
        return SUCCESS;
    }

    @Override
    public MedicineVO getModel() {
        return medicineVO;
    }

    @Resource
    public void setMedicineService(MedicineService medicineService) {
        this.medicineService = medicineService;
    }

    private Medicine copyDefaultMedicineInfo(Medicine targetMedicine, MedicineVO medicineVO) {
        targetMedicine.setAdaptation(medicineVO.getAdaptation());
        targetMedicine.setCaution(medicineVO.getCaution());
        targetMedicine.setCharacters(medicineVO.getCharacters());
        targetMedicine.setComponents(medicineVO.getComponents());
        targetMedicine.setDosage(medicineVO.getDosage());
        targetMedicine.setMedicineEnglishName(medicineVO.getMedicineEnglishName());
        targetMedicine.setMedicineName(medicineVO.getMedicineName());
        targetMedicine.setSideEffects(medicineVO.getSideEffects());
        targetMedicine.setTaboo(medicineVO.getTaboo());
        targetMedicine.setSort(medicineVO.getSort());
        targetMedicine.setType(medicineVO.getType());
        return targetMedicine;
    }
}
